let bankcard = require('../model/bankcard')
let {
    Router
} = require("express");
let router = new Router();
router.use(require("../middleware/auth.js"));
//建档用户绑定银行卡
router.post('/addbank', async (req, res) => {
    let userid = req.userInfo._id;
    let {
        name,
        bankid,
        bankname,
        phonenumber,
        idcard
    } = req.body;
    await bankcard.create({
        userid,
        name,
        bankid,
        bankname,
        phonenumber,
        idcard
    })
    res.status(200).json({
        msg: '添加成功',
    })
})

//查询个人银行卡
router.post('/inquirebank', async (req, res) => {
    let userid = req.userInfo._id;
    let data = await bankcard.find({
        userid
    })
    res.status(200).json({
        msg: '查询成功',
        data
    })
})

//根据银行卡id获得单张银行卡信息
router.post('/getbankinfo', async (req, res) => {
    let {
        id
    } = req.body;
    let data = await bankcard.findOne({
        _id: id
    })
    res.status(200).json({
        data
    })
})

//根据用户名和身份证获得单张银行卡信息
router.post('/getbankbyid', async (req, res) => {
    let {
        name,
        idcard,
        bankid
    } = req.body;
    let data = await bankcard.findOne({
        name,
        idcard,
        bankid
    })
    res.status(200).json({
        data
    })
})

router.post('/delbank', async (req, res) => {
    let {
        name,
        idcard,
        bankid
    } = req.body;
    await bankcard.deleteOne({
        name,
        idcard,
        bankid
    })
    res.status(200).json({
        msg: '删除成功'
    })
})
module.exports = router;